package com.sand.controller;

import com.alibaba.fastjson.JSON;
import com.sand.entity.ContractperformEntity;
import com.sand.service.IContractService;
import com.sand.util.ModelTest;
import com.sand.util.PageModel;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping(value = "contract")
public class ContractController {
/**
 *
 *
 * @author <achao1441470436@gmail.com>
 * @since 2021/12/6/006 22:38
 */
    //contractService接口
    @Resource(name = "contractService")
    private IContractService contractService;

    //分页查询合同
    @RequestMapping(value = "/queryContractLimit")
    public void queryContractLimit(int limit, int page,String condition,HttpServletResponse response)throws Exception{
        response.setCharacterEncoding("utf-8");

        PageModel<HashMap<String, Object>> pm = contractService.queryContractLimit(limit, page, condition);
        ModelTest<List<HashMap<String,Object>>> listData = new ModelTest<List<HashMap<String,Object>>>("OK",pm.getList(),0, pm.getPagecount());
        response.getWriter().write(JSON.toJSON(listData).toString());
    }
    //根据修改合同审核状态
    @RequestMapping(value = "/updateContractStatus")
    public void updateContractStatus(int rid,int status,HttpServletResponse response)throws Exception{
        boolean bln = contractService.updateContractStatus(rid,status);
        if(bln){
            response.getWriter().write("OK");
        }else{
            response.getWriter().write("ON");
        }
    }

    /**
     *
     * @param limit
     * @param page
     * @param condition
     * @param response
     * @throws Exception
     */
    @RequestMapping(value = "/queryContractByUseridLimit")
    public void queryContractByUseridLimit(int limit, int page,String condition,HttpServletResponse response)throws Exception{
        response.setCharacterEncoding("utf-8");
        PageModel<HashMap<String, Object>> pm = contractService.queryContractByUseridLimit(limit, page, condition);
        response.getWriter().write(JSON.toJSON(pm).toString());
        System.out.println(JSON.toJSON(pm).toString());
    }

    @RequestMapping(value = "/queryTenantContractViewLimit")
    public void queryTenantContractViewLimit(int limit, int page,String condition,HttpServletResponse response)throws Exception{
        response.setCharacterEncoding("utf-8");
        PageModel<HashMap<String, Object>> pm = contractService.queryTenantContractViewLimit(limit, page, condition);
        response.getWriter().write(JSON.toJSON(pm).toString());
        System.out.println(JSON.toJSON(pm).toString());
    }
    //用户查询账单
    @RequestMapping(value = "/queryBillByUser")
    public void queryBillByUser(int rid, int isAll,HttpServletResponse response)throws Exception{
        response.setCharacterEncoding("utf-8");
        List<ContractperformEntity> list = contractService.queryBillByUser(rid, isAll);
        response.getWriter().write(JSON.toJSON(list).toString());
    }
    //根据id查询合同详细信息
    @RequestMapping(value = "/queryContractById")
    public ModelAndView queryContractById(int rid){
        return new ModelAndView("layui/page/table_contract_show").addObject("contractShow", contractService.queryContractById(rid));
    }

    //根据id查询执行合同
    @RequestMapping(value = "/queryBillByPlayId")
    public ModelAndView queryBillByPlayId(int rid){
        return new ModelAndView("renting/pages/tenant/payment").addObject("playShow", contractService.queryBillByPlayId(rid));
    }

    //客户查询合同详细信息
    @RequestMapping(value = "/queryContractByLand")
    public ModelAndView queryContractByLand(int rid){
        return new ModelAndView("renting/pages/landlord/contract_show").addObject("contractShow", contractService.queryContractByLand(rid));
    }

    //根据合同id查询账单
    @RequestMapping(value = "/queryBillById")
    public ModelAndView queryBillById(int rid,int showWi){
        HashMap<String, Object> map = contractService.queryBillById(rid);
        ModelAndView mv = new ModelAndView();
        mv.addObject("billShow",map);
        switch (showWi){
            case 1://后台
                mv.setViewName("layui/page/table_bill_show");
                break;
            case 2://前台
                mv.setViewName("");
                break;
        }
        return mv;
    }




}
